﻿using System;
using System.Collections.Generic;
using System.Text;
using Cn.Cellula.Biz.Port;
using Cn.Cellula.Model;
using System.Reflection;
using Cn.Cellula.Model.System;

namespace Cn.Cellula.Biz.Impl {
    class AreaBiz : MyBizSupport,IAreaBiz {
        #region IAreaBiz 成员

        public IList<TSysAreaProvinceEO> LoadProvinceByCountry(string rCountryKey) {
            String hql = " from TSysAreaProvinceEO eo where eo.CountryKey='" + rCountryKey + "' ";
            return base.Find<TSysAreaProvinceEO>(hql);
        }

        public IList<TSysAreaCityEO> LoadCityByProvince(string rProvinceKey) {
            String hql = " from TSysAreaCityEO eo where eo.ProvinceKey='" + rProvinceKey + "' ";
            return base.Find<TSysAreaCityEO>(hql);
        }

        public string LoadArea2Json() {
            String hql = " from VSysAreaEO eo order by CountryKey,ProvinceKey,AreaKey ";
            IList<VSysAreaEO> list =  base.Find<VSysAreaEO>(hql);
            StringBuilder sb = new StringBuilder();
            String _couKey = null;
            String _prokey = null;
            int _couCount = 0;
            int _proCount = 0;
            int _citCount = 0;
            Boolean _couOpen = false;
            Boolean _proOpen = false;

            sb.Append("[");
            foreach (VSysAreaEO eo in list) {
                if (_couKey != eo.CountryKey) {
                    _couKey = eo.CountryKey;
                    if (_couCount > 0) {
                        sb.Append("\n]}");
                        sb.Append("\n,");
                        _couOpen = false;
                    }
                    sb.Append("\n{\"Key\":\"" + eo.CountryKey + "\",\"Name\":\"" + eo.CountryName + "\",\"Sub\":[");
                    _couOpen = true;
                    _proCount = 0;
                }
                if (_prokey != eo.ProvinceKey) {
                    if (_proCount > 0) {
                        sb.Append("\n\t]}");
                        _proOpen = false;
                        sb.Append(",");
                    }
                    sb.Append("\n\t{\"Key\":\"" + eo.ProvinceKey + "\",\"Name\":\"" + eo.ProvinceName + "\",\"Sub\":[");
                    _prokey = eo.ProvinceKey;
                    _proCount++;
                    _proOpen = true;
                    _citCount = 0;
                }
                if (_citCount > 0) {
                    sb.Append(",");
                }
                sb.Append("\n\t\t{\"Key\":\"" + eo.AreaKey + "\",\"Name\":\"" + eo.CityName + "\",\"Sub\":[]}");
                _citCount++;
            }
            if (_proOpen) sb.Append("\n\t]}");
            if (_couOpen) sb.Append("\n]}");
            sb.Append("\n]");
            return sb.ToString();
        }

        #endregion
    }
}
